package screens;

import gameobjects.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import levels.PngLevel;
import resources.Music;
import resources.Sound;
import resources.Sprite;
import tools.GameApplet;
import abstracts.Level;
import abstracts.Screen;

/** */
public class MenuScreen extends Screen {
    /** */
    private Button newGameButton;
    /** */
    private Button loadGameButton;
    /** */
    private Button howToPlayButton;

    /**
     * @param gameApplet - the {@link GameApplet} which this screen is
     *            to be displayed in.
     */
    public MenuScreen(final GameApplet gameApplet) {
        super(gameApplet);
    }

    @Override
    public final void begin() {
        Music.set(Music.MAIN_MENU);

        newGameButton = new Button(Sprite.NEW_GAME_BUTTON, getWidth() / 2, 200,
                new Runnable() {
                    @Override
                    public final void run() {
                        Sound.TELEPORT.play();
                        getApplet().setScreen(
                                new PngLevel(getApplet(), Sprite.LEVEL_TEST));
                    }
                });
        getApplet().getCanvas().addMouseListener(newGameButton);
        getApplet().getCanvas().addMouseMotionListener(newGameButton);

        loadGameButton = new Button(Sprite.LOAD_GAME_BUTTON, getWidth() / 2,
                300, new Runnable() {
                    @Override
                    public final void run() {
                        // TODO load game.
                    }
                });
        getApplet().getCanvas().addMouseListener(loadGameButton);
        getApplet().getCanvas().addMouseMotionListener(loadGameButton);

        howToPlayButton = new Button(Sprite.HOW_TO_PLAY_BUTTON, getWidth() / 2,
                400, new Runnable() {
                    @Override
                    public final void run() {
                        // TODO show how to play screen.
                    }
                });
        getApplet().getCanvas().addMouseListener(howToPlayButton);
        getApplet().getCanvas().addMouseMotionListener(howToPlayButton);
    }

    @Override
    public final void draw(final Graphics g) {
        final int fontSize = 48;
        final int titleY = 64;
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.WHITE);
        g.setFont(new Font("Georgia", Font.BOLD, fontSize));
        int w = g.getFontMetrics().stringWidth("Merlin's Revenge");
        g.drawString("Merlin's Revenge", (getWidth() - w) / 2, titleY);

        newGameButton.draw(g);
        loadGameButton.draw(g);
        howToPlayButton.draw(g);
    }

    @Override
    public final void end() {
        getApplet().getCanvas().removeMouseListener(newGameButton);
        getApplet().getCanvas().removeMouseMotionListener(newGameButton);

        newGameButton = null;
        getApplet().getCanvas().removeMouseListener(loadGameButton);
        getApplet().getCanvas().removeMouseMotionListener(loadGameButton);
        loadGameButton = null;

        getApplet().getCanvas().removeMouseListener(howToPlayButton);
        getApplet().getCanvas().removeMouseMotionListener(howToPlayButton);
        howToPlayButton = null;
    }

    @Override
    public final int getHeight() {
        return Level.HEIGHT;
    }

    @Override
    public final int getWidth() {
        return Level.WIDTH;
    }
}
